package com.cqc.googleplay.adapter;

import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.application.ImageLoaderOptions;
import com.cqc.googleplay.application.MyApplication;
import com.cqc.googleplay.bean.AppInfoBean;
import com.cqc.googleplay.http.NetUrl;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class HomeFragAdapter extends BaseListAdapter<AppInfoBean> {

	public HomeFragAdapter(List<AppInfoBean> list) {
		super(list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(parent.getContext(), R.layout.item_home_frag, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		AppInfoBean info = list.get(position);

		ImageLoader.getInstance().displayImage(NetUrl.IMAGE_PREFIX + info.getIconUrl(), holder.iv, ImageLoaderOptions.options);
		holder.tv_name.setText(info.getName());
		holder.tv_size.setText("" + Formatter.formatFileSize(MyApplication.getContext(), info.getSize()));
		holder.tv_desc.setText(info.getDes());
		holder.ratingBar.setRating(info.getStars());

		return convertView;
	}

	public class ViewHolder {

		public ImageView iv;
		public RatingBar ratingBar;
		public TextView tv_name;
		public TextView tv_size;
		public TextView tv_desc;

		public ViewHolder(View itemView) {
			super();
			iv = (ImageView) itemView.findViewById(R.id.iv_item_frag_home);
			ratingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
			tv_name = (TextView) itemView.findViewById(R.id.tv_name_frag_home);
			tv_size = (TextView) itemView.findViewById(R.id.tv_app_size_frag_home);
			tv_desc = (TextView) itemView.findViewById(R.id.tv_app_desc_frag_home);
		}
	}
}
